<?php


namespace app\admin\model\management;


use app\common\model\TimeModel;
use app\common\constants\MenuConstant;

use think\model\Pivot;
class Lease extends TimeModel
{
    protected $name = 'lease';
   
    public function setSpecsAttr($value)
    {
        if($value){
            if(is_array($value)) return implode(',', $value); else return $value;
        }else{
            return "";
        }
        
    }

    public function setSkusAttr($value)
    {
        return json_encode($value);
    }

    public function setSpecsValueAttr($value)
    {

        return json_encode($value);
    }
      public static function onAfterInsert($data)
    {   
       
        $data=$data->toArray();
        
        if(!empty($data['lease_value'])){
            foreach ($data['lease_value'] as $k => $v) {
            
                $data['lease_value'][$k]['lease_id']=$data['id'];
                
            }
            $SpecItem=new LeasePrice();
            $SpecItem->saveAll($data['lease_value']);
        }
        
    }
     public static function onAfterUpdate($data)
        {   
        
            $data=$data->toArray();
            
            if(!empty($data['lease_value'])){
                foreach ($data['lease_value'] as $k => $v) {
                
                    $data['lease_value'][$k]['lease_id']=$data['id'];
                    
                }
                $SpecItem=new LeasePrice();
                $SpecItem->where('lease_id',$data['id'])->delete();
                $SpecItem->saveAll($data['lease_value']);
            }

            
        }
      
}